package com.wing.product.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.product.dao.WarehouseOutboundOrderDao;
import com.wing.product.dao.WarehouseProductionSchedulingPlanDetailDao;
import com.wing.product.dao.WarehouseStorageOrderDao;
import com.wing.product.model.entity.WarehouseOutboundOrder;
import com.wing.product.model.entity.WarehouseProductionSchedulingPlanDetail;
import com.wing.product.model.response.WarehouseProductionSchedulingPlanDetailVO;
import com.wing.product.model.response.WarehouseStorageOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Service
public class WarehouseProductionSchedulingPlanDetailServiceImpl extends ServiceImpl<WarehouseProductionSchedulingPlanDetailDao, WarehouseProductionSchedulingPlanDetail> implements WarehouseProductionSchedulingPlanDetailService{

    @Autowired
    WarehouseStorageOrderDao warehouseStorageOrderDao;
    @Autowired
    WarehouseOutboundOrderDao warehouseOutboundOrderDao;
    @Override
    public Page<WarehouseProductionSchedulingPlanDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseProductionSchedulingPlanDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    @Transactional
    public JsonResult updateProductionSchedulingPlanDetail(String id, String status){

        WarehouseProductionSchedulingPlanDetail warehouseProductionSchedulingPlanDetail = this.baseMapper.selectById(id);
        warehouseProductionSchedulingPlanDetail.setStatus(status);
        warehouseProductionSchedulingPlanDetail.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(warehouseProductionSchedulingPlanDetail);
        return JsonResult.success("操作成功");
    }

    @Override
    public JsonResult<Map<Object, Object>> selectProductionSchedulingPlan(){
        Map<Object,Object> orderMap = new HashMap<>();
//        List<WarehouseProductionSchedulingPlanDetailVO> warehouseProductionSchedulingPlanDetailVOS = this.baseMapper.selectProductionSchedulingPlan();
        List<Map> rateZLD=baseMapper.rateZLD();
//        orderMap.put("test",warehouseProductionSchedulingPlanDetailVOS);
        orderMap.put("ProductionSchedulingPlan",rateZLD);

        //成品入库
        List<WarehouseStorageOrderVO> listStorageOrder = warehouseStorageOrderDao.list4appTwo(new Page<>(), new HashMap());

        //出库订单
        LambdaQueryWrapper<WarehouseOutboundOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(WarehouseOutboundOrder::getIsDeleted,0);
        List<WarehouseOutboundOrder> warehouseOutboundOrders = warehouseOutboundOrderDao.selectList(orderLambdaQueryWrapper);

        LocalDate today = LocalDate.now();
        LocalDate sevenDaysAgo = today.minusDays(6);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 使用年月日格式作为key
        Map<String, Long> dateCountMap = listStorageOrder.stream()
                .filter(inventory -> !inventory.getCreateTime().toLocalDate().isBefore(sevenDaysAgo)
                        && !inventory.getCreateTime().toLocalDate().isAfter(today))
                .collect(Collectors.groupingBy(
                        inventory -> inventory.getCreateTime().format(formatter), // 将LocalDateTime格式化为年月日
                        TreeMap::new,
                        Collectors.counting()
                ));

        Map<String, Long> dateOutboundOrderMapOut = warehouseOutboundOrders.stream()
                .filter(inventory -> !inventory.getCreateTime().toLocalDate().isBefore(sevenDaysAgo)
                        && !inventory.getCreateTime().toLocalDate().isAfter(today))
                .collect(Collectors.groupingBy(
                        inventory -> inventory.getCreateTime().format(formatter), // 将LocalDateTime格式化为年月日
                        TreeMap::new,
                        Collectors.counting()
                ));
        // 填充缺失的日期，确保前七天都有值
        for (LocalDate date = sevenDaysAgo; !date.isAfter(today); date = date.plusDays(1)) {
            String formattedDate = date.format(formatter);
            dateCountMap.putIfAbsent(formattedDate, 0L);
            dateOutboundOrderMapOut.putIfAbsent(formattedDate, 0L);
        }
        orderMap.put("StorageOrder",dateCountMap);
        orderMap.put("OutboundOrder",dateOutboundOrderMapOut);
        return JsonResult.success(orderMap);
    }
}
